import requests
from bs4 import BeautifulSoup as bs
from pyquery import PyQuery as pq
import json
import re
from sql_db_music import MySqlHelper
USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36'
headers = {
    'User-Agent': USER_AGENT
}


def read_song_list(url, item_dict_all=None):
    if item_dict_all is None:
        item_dict_all = list()
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        # titles = re.compile('collected_count.*?title\":"(.*?)\"', re.S) # 标题
        # title = re.findall(titles, response.text)
        # imgs = re.compile('title.*?\"cover\":\"(.*?)\"', re.S)
        # img = re.findall(imgs, response.text)
        # print(title)
        music = json.loads(response.text)
        num = 0
        for i in music:
            num += 1
            if num > 10:
                break
            music = list()
            music.append(i['title'])  # 标题
            music.append(i['cover'])  # 图片
            music.append('https://douban.fm/songlist/' + str(i['id']))  # 详情页地址
            item_dict_all.append(music)
    else:
        print("你抓的出问题了！")
    return item_dict_all


if __name__ == "__main__":
    for i in [0, 4, 5]:
        url = "https://douban.fm/j/v2/songlist/explore?type=hot&genre=" + str(i) + "&limit=20&sample_cnt=5"

        item_dict_all = read_song_list(url)
        print(url)
        mysql = MySqlHelper()
        sql = "INSERT INTO db_music(title, img, content) VALUES(%s,%s,%s)"
        mysql.exec_many(sql, item_dict_all)
